<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <!-- 搜索区域 -->
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="10" :sm="12">
            <a-form-item label="用户账号" style="margin-left:8px">
              <a-input v-model="queryParam.username" placeholder="请输入名称查询" />
            </a-form-item>
          </a-col>
          <!--<a-col :md="8" :sm="8">-->
          <!--<a-form-item label="用户名称" :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}">-->
          <!--<a-input placeholder="请输入名称查询" v-model="queryParam.realname"></a-input>-->
          <!--</a-form-item>-->
          <!--</a-col>-->
          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="6" :sm="24">
              <a-button type="primary" icon="search" style="margin-left: 18px" @click="searchQuery">查询</a-button>
              <a-button type="primary" icon="reload" style="margin-left: 8px" @click="searchReset">重置</a-button>
            </a-col>
          </span>
        </a-row>
      </a-form>
    </div>
    <!-- 操作按钮区域 -->
    <div class="table-operator" :md="24" :sm="24" style="margin: -10px 0px 10px 10px">
      <!-- <a-button type="primary" icon="plus" style="margin-top: 16px" @click="handleAdd">用户录入</a-button>
      <a-button type="primary" @click="handleEdit" icon="edit" style="margin-top: 16px">用户编辑</a-button> -->
      <a-button type="primary" icon="plus" @click="handleAddUserDepart">添加已有用户</a-button>

      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除关系
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon" /> 已选择 <a style="font-weight: 600">{{
          selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        row-key="id"
        :columns="columns"
        :data-source="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :row-selection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange"
      >

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />

          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a href="javascript:;" @click="handleDetail(record)">详情</a>
              </a-menu-item>

              <a-menu-item>
                <a-popconfirm title="确定要删除关系吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除关系</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <user-modal ref="modalForm" @ok="modalFormOk" />
    <Select-User-Modal ref="selectUserModal" @selectFinished="selectOK" />
  </a-card>
</template>

<script>
import { skylineListMixin } from '@/pages/mixins/skylineListMixin'
import { postAction } from '@/api/manage'
import SelectUserModal from './SelectUserModal'
import UserModal from './UserModal'

export default {
  name: 'DeptUserInfo',
  components: {
    SelectUserModal,
    UserModal
  },
  mixins: [skylineListMixin],
  data () {
    return {
      description: '用户信息',
      currentDeptId: '',
      // 表头
      columns: [{
        title: '用户账号',
        align: 'center',
        dataIndex: 'username'
      },
      {
        title: '用户手机',
        align: 'center',
        dataIndex: 'mobile'
      },
      {
        title: '操作',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' },
        align: 'center',
        width: 170
      }],
      url: {
        list: '/auth/sysUser/departUserList',
        edit: '/auth/sysUser/editSysDepartWithUser'
      }
    }
  },
  created () {
  },

  methods: {

    loadData (arg) {
      // 加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      if (this.currentDeptId === '') return
      // var params = this.getQueryParams()// 查询条件
      var params = {}
      params.depId = this.currentDeptId
      params.username = this.username
      params.pageNo = this.ipagination.current
      params.pageSize = this.ipagination.pageSize
      this.$authApi.getDepartUserList(params).then((res) => {
        if (res.code === '000000') {
          this.dataSource = res.data.records
          this.ipagination.total = res.data.total
        }
      })
    },
    batchDel: function () {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        var that = this
        console.log(this.currentDeptId)
        this.$confirm({
          title: '确认删除',
          content: '是否删除选中数据?',
          onOk: function () {
            that.$authApi.deleteUserInDepartBatch(that.currentDeptId, ids).then((res) => {
              if (res.code === '000000') {
                that.$message.success(res.mesg)
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.mesg)
              }
            })
          }
        })
      }
    },
    handleDelete: function (id) {
      var that = this
      this.$authApi.deleteUserInDepart(this.currentDeptId, id).then((res) => {
        if (res.code === '000000') {
          that.$message.success(res.mesg)
          that.loadData()
        } else {
          that.$message.warning(res.mesg)
        }
      })
    },
    open (record) {
      // console.log(record);
      this.currentDeptId = record.id
      this.loadData(1)
    },
    clearList () {
      this.currentDeptId = ''
      this.dataSource = []
    },
    hasSelectDept () {
      if (this.currentDeptId == null) {
        this.$message.error('请选择一个部门!')
        return false
      }
      return true
    },
    handleAddUserDepart () {
      if (this.currentDeptId === '') {
        this.$message.error('请选择一个部门!')
      } else {
        this.$refs.selectUserModal.visible = true
      }
    },
    handleAdd: function () {
      if (this.currentDeptId === '') {
        this.$message.error('请选择一个部门!')
      } else {
        this.$refs.modalForm.departDisabled = true
        this.$refs.modalForm.deptId = this.currentDeptId
        this.$refs.modalForm.add()
        this.$refs.modalForm.title = '新增'
      }
    },
    selectOK (data) {
      const params = {}
      params.depId = this.currentDeptId
      params.userIdList = []
      for (var a = 0; a < data.length; a++) {
        params.userIdList.push(data[a])
      }
      console.log(params)
      postAction(this.url.edit, params).then((res) => {
        if (res.data.code === '000000') {
          this.$message.success(res.data.mesg)
          this.loadData()
        } else {
          this.$message.warning(res.data.mesg)
        }
      })
    }
  }
}
</script>
<style scoped>
  /** Button按钮间距 */
  .ant-btn {
    margin-left: 3px
  }

  .ant-card {
    margin-left: -30px;
    margin-right: -30px;
  }

  .table-page-search-wrapper {
    margin-top: -16px;
    margin-bottom: 16px;
  }
</style>
